package cn.edu.uestc.smgt.util;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

/**
 * 生成excel视图，可用excel工具打开或者保存 由ViewController的return new ModelAndView(viewExcel,
 * model)生成
 * 
 * @author Tony Lin Created on 2008-10-22
 * @version Version 1.0
 */
@SuppressWarnings("deprecation")
public class ViewExcel extends AbstractExcelView {

	public void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HSSFSheet sheet = workbook.createSheet("list");
		sheet.setDefaultColumnWidth((short) 12);

		HSSFCell cell = getCell(sheet, 0, 0);
		setText(cell, "ZGBH");
		cell = getCell(sheet, 0, 1);
		setText(cell, "ZGXM");
		cell = getCell(sheet, 0, 2);
		setText(cell, "DWBH");
		cell = getCell(sheet, 0, 3);
		setText(cell, "DWMC");
		cell = getCell(sheet, 0, 4);
		setText(cell, "KSMC");
		cell = getCell(sheet, 0, 5);
		setText(cell, "YHMC");
		cell = getCell(sheet, 0, 6);
		setText(cell, "JHKH");
		cell = getCell(sheet, 0, 7);
		setText(cell, "LXDH");
		cell = getCell(sheet, 0, 8);
		setText(cell, "NY");
		cell = getCell(sheet, 0, 9);
		setText(cell, "JE");
		cell = getCell(sheet, 0, 10);
		setText(cell, "BZ");

		HSSFCellStyle dateStyle = workbook.createCellStyle();
		// dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy"));
		List data = (List) model.get("list");
		String ny = (String) model.get("date");
		int i = 1, j = 0;
		for (Object object : data) {
			j = 0;
			HashMap map = (HashMap) object;
			cell = getCell(sheet, i, j++);
			setText(cell, (String) map.get("id"));
			cell = getCell(sheet, i, j++);
			setText(cell, (String) map.get("name"));
			cell = getCell(sheet, i, j++);
			setText(cell, (String) map.get("unitId"));
			cell = getCell(sheet, i, j++);
			setText(cell, (String) map.get("unitName"));
			cell = getCell(sheet, i, j++);
			setText(cell, (String) map.get("officeName"));
			cell = getCell(sheet, i, j++);
			setText(cell, "中国建设银行");
			cell = getCell(sheet, i, j++);
			setText(cell, (String) map.get("bankNo"));
			cell = getCell(sheet, i, j++);
			setText(cell, (String) map.get("tel"));
			cell = getCell(sheet, i, j++);
			setText(cell, ny);
			i++;
		}
		String filename = "out.xls";// 设置下载时客户端Excel的名称
		// filename = MyUtils.encodeFilename(filename, request);//处理中文文件名
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename=" + filename);
		OutputStream ouputStream = response.getOutputStream();
		workbook.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();
	}

	/**
	 * 设置下载文件中文件的名称
	 * 
	 * @param filename
	 * @param request
	 * @return
	 */
	/**
	 * 获取客户端浏览器和操作系统信息 在IE浏览器中得到的是：User-Agent=Mozilla/4.0 (compatible; MSIE 6.0;
	 * Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
	 * 在Firefox中得到的是：User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN;
	 * rv:1.7.10) Gecko/20050717 Firefox/1.0.6
	 */
	/*
	 * public static String encodeFilename(String filename, HttpServletRequest
	 * request) {
	 * 
	 * String agent = request.getHeader("USER-AGENT"); try { if ((agent != null)
	 * && (-1 != agent.indexOf("MSIE"))) { String newFileName =
	 * URLEncoder.encode(filename, "UTF-8"); newFileName =
	 * StringUtils.replace(newFileName, "+", "%20"); if (newFileName.length() >
	 * 150) { newFileName = new String(filename.getBytes("GB2312"),
	 * "ISO8859-1"); newFileName = StringUtils.replace(newFileName, " ", "%20");
	 * } return newFileName; } if ((agent != null) && (-1 !=
	 * agent.indexOf("Mozilla"))) return MimeUtility.encodeText(filename,
	 * "UTF-8", "B");
	 * 
	 * return filename; } catch (Exception ex) { return filename; } }
	 */
}